package cn.kgc.base.controller;

import cn.kgc.base.service.FileService;
import cn.kgc.commons.config.OssFileConfig;
import cn.kgc.commons.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @description: 文件上传控制器
 * @author: 石浩杰
 * @date: 2022-11-29 08:28
 */
@RestController
@Api
@RequestMapping("/oss")
@ConditionalOnClass(ResponseBodyAdvice.class)
public class OssFileController {
    @Resource(name = "ossFileService")
    private FileService fileService;

    @PostMapping("/upload")
    @CrossOrigin
    @ApiOperation(value = "oss的文件上传",httpMethod = "POST",response = Result.class,produces = "application/json;charset=utf-8")
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        //todo 从请求获得用户ID
        String userId="0";
        String path=fileService.upload(file,userId,file.getOriginalFilename());
        return Result.ok(path,request.getRequestURL().toString());
    }
}
